/**
 * npm webpack webpack-dev-server exports-loader url-loader uglify-loader file-loader style-loader css-loader postcss-loader babel-loader babel-core babel-preset-es2015
 * jshint cssnext postcss-atroot postcss-conditionals postcss-custom-media postcss-each postcss-extend postcss-for postcss-import postcss-mixins postcss-nested
 * postcss-simple-vars postcss-sprites extract-text-webpack-plugin html-loader ngtemplate-loader
 */


// Load plugins
var webpack           = require('webpack'),                                             //webpack
    fs                = require('fs'),                                                  //文件模块
    path              = require('path'),                                                //路径 
    cssImport         = require("postcss-import"),                                      //import
    cssMixins         = require("postcss-mixins"),                                      //函数
    cssExtend         = require("postcss-extend"),                                      //继承
    conditionals      = require("postcss-conditionals"),                                //条件语句
    cssEach           = require("postcss-each"),                                        //each
    cssFor            = require("postcss-for"),                                         //for
    nested            = require('postcss-nested'),                                      //CSS代码嵌套
    cssSimpleVars     = require("postcss-simple-vars"),                                 //变量
    customMedia       = require("postcss-custom-media"),                                //media
    cssAtroot         = require("postcss-atroot"),                                      //atroot
    cssnext           = require('cssnext'),                                             //CSS4+语法
    sprites           = require('postcss-sprites'),                                     //sprites 
    ExtractTextPlugin = require("extract-text-webpack-plugin"),                         //css打包
    optimize          = webpack.optimize,                                               //webpack插件
    commonsPlugin     = new optimize.CommonsChunkPlugin('common.js'),                   //将公共代码抽离出来合并为一个文件
    uglifyJsPlugin    = new optimize.UglifyJsPlugin({compress: {warnings: false}});     //js文件的压缩


// 配置基本路径
var baseUrl = {
    src: 'sdk_src/',
    dist: '../../'
}


// 配置参数
var config = {

    // 生成.map文件便于调试
    devtool: 'source-map',

    // 页面入口文件配置
    entry: getEntry(),

    // 入口文件输出配置
    output: {
        path: path.resolve(__dirname, baseUrl.src + 'modules'),
        publicPath: baseUrl.dist,
        filename: '[name]/build/[name].js'
    },

    // 配置插件
    plugins: [
        commonsPlugin, 
        // uglifyJsPlugin,
        new ExtractTextPlugin("[name]/build/[name].css")
    ],

    // 配置模块
    module: {

        //加载器配置
        loaders: [

            // ES6转码
            { 
                test: /\.js$/, 
                loader: 'babel', 
                exclude: [path.resolve(__dirname, 'node_modules')], 
                query: { presets: 'es2015' } 
            },

            // .css 文件使用postcss来处理并打包
            { 
                test: /\.css$/, 
                loader: ExtractTextPlugin.extract('style', 'css!postcss') 
            },

            // 内联 base64 URLs, 限定 <=8k 的图片, 其他的用 URL
            { 
                test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/, 
                loader: 'url-loader?limit=8192&name=app/build/[name].[ext]' 
            }
        ]
    },

    // postcss
    postcss: function (webpack) {

        var dependent = {
            addDependencyTo: webpack
        }

        var processors = [
            cssImport(dependent),
            cssMixins(dependent),
            cssExtend(dependent),
            conditionals(dependent),
            cssEach(dependent),
            cssFor(dependent),
            nested(dependent),
            cssSimpleVars(dependent),
            customMedia(dependent),
            cssAtroot(dependent),
            cssnext(dependent)
        ];

        return processors;
    },

    // 其它解决方案配置
    resolve: {
        extensions: ['', '.js', '.json', '.css'] 
    }
}


// 页面入口文件配置
function getEntry() {
    
    var srcDir = './'+ baseUrl.src + 'modules';
    var jsPath = path.resolve(srcDir);
    var dirs   = fs.readdirSync(jsPath);
    var matchs = [], files = {};

    dirs.forEach(function (item) {

        if (!fs.lstatSync(srcDir + '/' + item).isDirectory()) {
            return false;
        }

        if (fs.lstatSync(srcDir + '/' + item).isDirectory()) {

            var app = fs.readdirSync(srcDir + '/' + item + '/app')

            matchs = /(.+)\.js$/.test(app[0])

            if (matchs) {
                // files[item] = srcDir + '/' + item + '/app/' + app[0];
                files[item] = path.resolve(srcDir, item + '/app/' + app[0]);
            }
        }

    });

    return files;
}

module.exports = config;